import 'package:flutter/material.dart';
import 'package:get/get.dart';

class Controller extends GetxController {
  //
  var count = 0.obs;
  var count2 = RxInt(0);
  var count3 = Rx<int>(0);
  //
  increment() => {
    count++,
    update(),
  };
  increment2() => count2++;
  int get sum => count.value + count2.value;
}

class TestGetX extends StatelessWidget {
  const TestGetX({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    final Controller c = Get.put(Controller());
    return Scaffold(
      appBar: AppBar(title: Obx(() => Text("Clicks: ${c.count}"))),
      body: Center(
        child: Center(
          child: Column(
            children: [
              GetBuilder<Controller>(
                builder: (s) => Text('${s.count}')
              ),
              GetX<Controller>(builder: (ctrl) {
                print("count 1 rebuild");
                return Text('${ctrl.count}');
              }),
              Obx(() => Text(c.count2.toString())),
              GetX<Controller>(builder: (ctrl) {
                print("count 3 rebuild");
                return Text('${ctrl.sum}');
              }),
              ElevatedButton(onPressed: c.increment, child: const Text('count1 +')),
              ElevatedButton(onPressed: c.increment2, child: const Text('count2 +')),
            ],
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () => Get.to(() => TestGetXOther()), 
        child: const Icon(Icons.next_plan)
      ),
    );
  }
}

class TestGetXOther extends StatelessWidget {
  final Controller c = Get.find();

  TestGetXOther({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(child: Text("${c.count}")),
      appBar: AppBar(title: const Text("other page")),
    );
  }
}